package com.imooc.news_manager_project.controller;

import com.imooc.news_manager_project.common.Result;
import com.imooc.news_manager_project.common.validator.SaveValidator;
import com.imooc.news_manager_project.dto.LoginDTO;
import com.imooc.news_manager_project.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "登录管理")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/index")
    public Result login(@RequestBody @Validated(SaveValidator.class) LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "用户登出")
    @PostMapping("/logout")
    public Result logout(@RequestHeader("token") String token) {
        return userService.logout(token);
    }
}
